﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Linq.Expressions;

namespace ExpressionTree和IL_Emit
{
    class Program
    {
        static void Main(string[] args)
        {
            //IFoo f;
            //Bar b = new Bar();
            //SetPropertyValueViaExpression(f, b);
        }
        static void SetPropertyValueViaExpression(IFoo f, Bar b)
        {
            var property = typeof(IFoo).GetProperty("Bar");
            var target = System.Linq.Expressions.Expression.Parameter(typeof(IFoo));
            var propertyValue = Expression.Parameter(typeof(Bar));
            var setPropertyValue = Expression.Call(target, property.GetSetMethod(), propertyValue);
            var setAction = Expression.Lambda<Action<IFoo, Bar>>(setPropertyValue, target, propertyValue).Compile();
            setAction(f, b);
        }
    }



    public interface IFoo
    {
        Bar aa{get;set;}
    }
    public class Bar { }


}
